﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chapter_8_Arrays
	{
	class Program
		{
		static void Main(string[] args)
			{
			int lowest = 0, highest = 0, total = 0, average = 0;
			int[] num = new int[20];
			string textnumbers = "";
			string[] numbers = new string[20];

			getNumbers(ref textnumbers, ref numbers);

			validate(numbers, num);

			calculate(ref lowest, ref highest, ref total, ref average, num);

			print("The lowest number is " + lowest + ", and the highest is " + highest + ".\nTheir total added value is " + total + ".\nThe average of the total is " + average + ".");

			}

		static void getNumbers(ref string textnumbers, ref string[] numbers)
			{
			print("Provide 20 WHOLE numbers separated by a space.\nIf you provide more than the asked ammount the will NOT be stored in the program");
			textnumbers = Console.ReadLine();
			numbers = textnumbers.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
			while (numbers.Length < 20)
				{
				print("You only provided " + numbers.Length + " value(s), enter " + (20 - numbers.Length) + " more number(s).\nANY MORE NUMBERS WILL NOT BE STORED ON THE PROGRAM.");
				textnumbers = textnumbers + " " + Console.ReadLine();
				numbers = textnumbers.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
				}
			}

		static void validate(string[] numbers, int[] num)
			{
			for (int i = 0; i < 20; i++)
				{
				while (!int.TryParse(numbers[i], out num[i]))
					{
					print("The " + wnum(i + 1) + " is not a valid number, provide a valid number");
					numbers[i] = Console.ReadLine();
					}
				}
			}

		static void calculate(ref int lowest, ref int highest, ref int total, ref int average, int[] num)
			{
			lowest = num[0];
			highest = num[0];
			for (int i = 0; i < 20; i++)
				{
				total = total + num[i];
				if (num[i] < lowest) lowest = num[i];
				if (num[i] > highest) highest = num[i];
				}
			average = total / 20;
			}

		static string wnum(int i)
			{
			switch (i % 10)
				{
				case 1:
					return i + "st";
				case 2:
					return i + "nd";
				case 3:
					return i + "rd";
				default:
					return i + "th";
				}
			}

		static void print(string p)
			{
			Console.WriteLine("\n" + p);
			}
		}
	}

